"""
用户数据模式（Schema）定义
处理用户相关的请求与响应数据结构
"""
from typing import Optional, List
from datetime import datetime
from pydantic import BaseModel, Field, field_validator
import re


class UserBase(BaseModel):
    """用户基础数据模型"""
    username: str = Field(..., min_length=3, max_length=50, description="用户名")
    email: str = Field(..., description="电子邮箱")
    phone: Optional[str] = Field(None, description="电话号码")
    full_name: Optional[str] = Field(None, max_length=100, description="全名")
    avatar: Optional[str] = Field(None, description="头像URL")
    
    @field_validator('email')
    def validate_email(cls, v):
        if not re.match(r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$', v):
            raise ValueError('无效的邮箱格式')
        return v
    
    @field_validator('phone')
    def validate_phone(cls, v):
        if v and not re.match(r'^\+?[0-9]{8,15}$', v):
            raise ValueError('无效的电话号码格式')
        return v


class UserCreate(UserBase):
    """创建用户请求模型"""
    password: str = Field(..., min_length=6, description="密码")
    
    @field_validator('password')
    def password_strength(cls, v):
        if len(v) < 6:
            raise ValueError('密码长度至少为6个字符')
        # if not any(char.isdigit() for char in v):
        #     raise ValueError('密码必须包含至少一个数字')
        # if not any(char.isupper() for char in v):
        #     raise ValueError('密码必须包含至少一个大写字母')
        return v


class UserUpdate(BaseModel):
    """更新用户请求模型(部分更新)"""
    username: Optional[str] = Field(None, min_length=3, max_length=50)
    email: Optional[str] = None
    phone: Optional[str] = None
    full_name: Optional[str] = None
    avatar: Optional[str] = None
    status: Optional[int] = None
    role: Optional[str] = None
    
    @field_validator('email')
    def validate_email(cls, v):
        if v and not re.match(r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$', v):
            raise ValueError('无效的邮箱格式')
        return v
    
    @field_validator('phone')
    def validate_phone(cls, v):
        if v and not re.match(r'^\+?[0-9]{8,15}$', v):
            raise ValueError('无效的电话号码格式')
        return v


class UserWholeUpdate(UserBase):
    """完整更新用户请求模型"""
    status: int = Field(..., description="用户状态")
    role: str = Field(..., description="用户角色")


class SetPasswordRequest(BaseModel):
    """设置密码请求"""
    password: str = Field(..., min_length=6, description="新密码")
    
    @field_validator('password')
    def password_strength(cls, v):
        if len(v) < 6:
            raise ValueError('密码长度至少为6个字符')
        # if not any(char.isdigit() for char in v):
        #     raise ValueError('密码必须包含至少一个数字')
        # if not any(char.isupper() for char in v):
        #     raise ValueError('密码必须包含至少一个大写字母')
        return v


class UserResponse(UserBase):
    """用户响应模型"""
    id: int
    status: int
    # role字段暂时设为可选
    # role: Optional[str] = None
    is_superuser: bool
    is_verified: bool
    created_at: datetime
    updated_at: datetime
    last_login: Optional[datetime] = None
    
    # 使用现代Pydantic v2配置风格
    model_config = {"from_attributes": True}


class UserListResponse(BaseModel):
    """用户列表响应模型"""
    items: List[UserResponse]
    total: int
    page: int
    page_size: int
    pages: int
